var userAgent = navigator.userAgent.toLowerCase();
var isIpad = userAgent.match(/ipad/i) == "ipad";

// if (!isIpad) alert('不是IPAD！');

var oldX6, oldY6, startX6, startY6, startWidth6, startHeight6;
var moveD6;
var isMove6 = false;
var isZoom6 = false;
var lastClickTime6 = 0;

// 获取两点之间的距离
function get_distance6(x1, y1, x2, y2)
{
    return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 2);
}

function img_mousedown6(e)
{
    if (e.target.id != 'img6') return;

    if (e.touches.length == 1)
    {
        var nowTime = Math.round(new Date().getTime() / 1000);
        x = $('#img6').position().left;
        y = $('#img6').position().top;
        if (nowTime - lastClickTime6 < 1 && Math.abs(x - startX6) < 20 && Math.abs(y - startY6) < 20)
        {
            // 在1秒内连续点击同一地方。
            //alert('双击事件');
        }
        lastClickTime6 = nowTime;
    }
    else if (e.touches.length >= 2)
    {
        isMove6 = false;
        isZoom6 = true;
        x1 = e.touches[0].pageX;
        y1 = e.touches[0].pageY;
        x2 = e.touches[1].pageX;
        y2 = e.touches[1].pageY;

        startX6 = $('#img6').position().left;
        startY6 = $('#img6').position().top;
        startWidth6 = $('#img6').width();
        startHeight6 = $('#img6').height();

        moveD6 = get_distance6(x1, y1, x2, y2);

        return;
    }

    isMove6 = true;
    oldX6 = e.touches[0].pageX;
    oldY6 = e.touches[0].pageY;
    startX6 = $('#img6').position().left;
    startY6 = $('#img6').position().top;
    e.preventDefault();
    e.stopPropagation();
    return false;
}

function img_mouseup6(e)
{
    if (e.target.id != 'img6') return;

    isZoom6 = false;
    isMove6 = false;
}

function img_mousemove6(e)
{
    if (isZoom6)
    {
        //targetTouches changedTouches touches
        if (e.touches.length >= 2)
        {
            var x1, y1, x2, y2, d1;
            x1 = e.touches[0].pageX;
            y1 = e.touches[0].pageY;
            x2 = e.touches[1].pageX;
            y2 = e.touches[1].pageY;
            d1 = get_distance6(x1, y1, x2, y2);
            var rate = d1 / moveD6;
            var w = startWidth6 * rate;
            var h = startHeight6 * rate;

            $('#img6').width(w);
            $('#img6').height(h);
            $('#img6').css('left', (startWidth6 - w) / 2 + startX6 + 'px');
            $('#img6').css('top', (startHeight6 - h) / 2 + startY6 + 'px');
        }

        return;
    }

    if (!isMove6) return;
    x = e.changedTouches[0].pageX - oldX6;
    y = e.changedTouches[0].pageY - oldY6;

    $('#img6').css('top', y + startY6 + 'px');
    $('#img6').css('left', x + startX6 + 'px');
}

$(function(){
    document.onmousemove6 = function (e)
    {
        var e = e || event;
        e.cancelBubble = true;
        e.returnValue = false;
    }

    // 防止触发浏览器的整体拖动
    document.body.addEventListener('touchmove', function (e)
    {
        e.preventDefault();
    }, false);

    document.addEventListener('touchstart', img_mousedown6, false);
    document.addEventListener('touchend', img_mouseup6, false);
    document.addEventListener('touchmove', img_mousemove6, false);
});
